<?php

/**
 * Code that must be executed for a teamug app to run
 * @author Anthony pf@teamug.net
 */


// Classes which must be preloaded as they are used in __autoload()
require_once __DIR__.'/Dev.php';
require_once __DIR__.'/Project.php';

use \teamug\Dev as Dev;
use \teamug\Project as Project;


/**
 * Load classes dynamically using __autoload()
 * Finds the correct directory from the class's namespace
 * Eg: app\Customer
 * 
 * @param string $className This will include namespace. ie ns\cls
  */
function __autoload($className) 
{
  $ns_bits = \explode('\\', $className); // Eg: ['app', 'database', 'Customer']
  $ns_root = \array_shift($ns_bits); // Eg: 'app'
 
  if ($ns_root==='teamug') {
    $dir = __DIR__; 
  }
  else {
    $prj = Project::getSingleton();
    $dir = $prj->getNamespaceDirectory($ns_root); 
  }
  
  $file_path = $dir.'/'.implode('/', $ns_bits).'.php';
  
  \teamug\Dev::narrate("Autoloading file:'$file_path' to find class:'$className'");
  
  require_once $file_path;
}
